package com.holly.common_view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.RadioButton;

/**
 * 单选换行
 * 2016/12/10 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class AutoWrapSingleChoiceLayout extends AutoWrapLayout {
    protected OnSingleCheckedChangedListener mOnSingleCheckedChangedListener;

    public AutoWrapSingleChoiceLayout(Context context) {
        super(context);
    }

    public AutoWrapSingleChoiceLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoWrapSingleChoiceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int mCheckedPosition = -1;

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //设置默认选中第一个
    }

    @Override
    public void notifyDataChanged() {
        super.notifyDataChanged();
        mCheckedPosition = -1;
        //刷新完后设置Checked为第一个
        for (int i = 0; i < mChildViewList.size(); i++) {
            if (mChildViewList.get(i) instanceof RadioButton) {
                RadioButton radioButton = (RadioButton) mChildViewList.get(i);
                radioButton.setOnCheckedChangeListener(null);
                if (mCheckedPosition == -1) {
                    radioButton.setChecked(true);
                    mCheckedPosition = i;
                    if (mOnSingleCheckedChangedListener != null) {
                        mOnSingleCheckedChangedListener.onCheckedPositionChanged(radioButton, mCheckedPosition);
                    }
                } else {
                    radioButton.setChecked(false);
                }
                radioButton.setOnCheckedChangeListener(new SimpleSingleCheckedChangedListener(i));
            }
        }
    }

    public void setCheckedPosition(int position) {
        if (position < mChildViewList.size()) {
            if (mCheckedPosition != position) {
                RadioButton view = (RadioButton) mChildViewList.get(position);
                view.setChecked(true);//取消别的选项，这个在监听中自动完成了
            }
        } else {
            throw new IndexOutOfBoundsException("setCheckedPosition index = " + position + " but child count = " + mChildViewList.size());
        }
    }

    public int getCheckedPosition() {
        return mCheckedPosition;
    }

    public void setOnSingleCheckedChangedListener(OnSingleCheckedChangedListener onSingleCheckedChangedListener) {
        mOnSingleCheckedChangedListener = onSingleCheckedChangedListener;
    }

    //单选逻辑在这个监听中
    class SimpleSingleCheckedChangedListener implements CompoundButton.OnCheckedChangeListener {
        private int mPosition;

        public SimpleSingleCheckedChangedListener(int position) {
            this.mPosition = position;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked && mPosition != mCheckedPosition) {
                if (mCheckedPosition != -1) {
                    CompoundButton button = (CompoundButton) mChildViewList.get(mCheckedPosition);
                    button.setChecked(false);
                }
                mCheckedPosition = mPosition;

                if (mOnSingleCheckedChangedListener != null) {
                    mOnSingleCheckedChangedListener.onCheckedPositionChanged(buttonView, mCheckedPosition);
                }
            }
        }
    }

    public interface OnSingleCheckedChangedListener {
        //选中的位置
        void onCheckedPositionChanged(CompoundButton buttonView, int position);
    }
}
